package com.demo.salary.transactionimplementation;

import com.demo.salary.payrolldomain.Employee;
import com.demo.salary.PayCheck;
import com.demo.salary.payrolldatabase.PayrollDatabase;
import com.demo.salary.transactionapplication.Transaction;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PayDayTransaction implements Transaction {

    private final LocalDate itsPayDate;
    Map<Integer, PayCheck> itsPaychecks = new HashMap<>();

    public PayDayTransaction(LocalDate itsPayDate) {
        this.itsPayDate = itsPayDate;
    }

    @Override
    public void execute() {
        List<Integer> allEmployeeIds = PayrollDatabase.getAllEmployeeIds();

        for (Integer id : allEmployeeIds) {
            Employee employee = PayrollDatabase.getEmployee(id);
            if (employee.isPayDate(itsPayDate)) {
                PayCheck pc = new PayCheck(employee.getPayPeriodStartDate(itsPayDate), itsPayDate);
                itsPaychecks.put(id, pc);
                employee.payDay(pc);
            }
        }
    }

    public PayCheck getPayCheck(int empId) {
        return itsPaychecks.get(empId);
    }
}
